package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


import model.FileTreatment;
import Thread.algo;
import model.SearchFile;

/**Controleur est une classe listerner d'action qui herite de JFrame*/
public class SwingControl extends JFrame implements ActionListener, KeyListener
{
    /**
	 * 
	 */
	private static final long serialVersionUID = -7412449560758350579L;
	private SearchFile searchFile;
    private FileTreatment fileTreatment;
    private String path;
    
    private JButton butt;
    private JLabel monteCarloLabel;
    private JTextField jtf;
        
    /**constructeur*/
    public SwingControl() {
        super();
        build();
    }
    
    private void build() {
        setTitle("Monte Carlo");
        
        setLocationRelativeTo(null);
        setSize(650,120);
        setResizable(false); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setContentPane(buildContentPane());
    }
    
    private JPanel buildContentPane()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBackground(Color.WHITE);
        
        butt = new JButton("Read a file");
        butt.addActionListener(this);
        panel.add(butt);
        
        jtf = new JTextField();
        jtf.addKeyListener(this);
        jtf.setPreferredSize(new Dimension(150,30));
        jtf.setColumns(15);
        Font police = new Font("Arial", Font.BOLD, 14);
        jtf.setFont(police);
        panel.add(jtf);
        
        monteCarloLabel = new JLabel("No file loaded for the moment");
        panel.add(monteCarloLabel);
        return panel;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        Object source = e.getSource();
        
        if((JButton)source == butt)
        {
        	searchFile = new SearchFile(this);
        	path = searchFile.ChooseFile(e);
        	fileTreatment = new FileTreatment();
			try {
				fileTreatment.loader(fileTreatment.read(path));
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
        	monteCarloLabel.setText("Preset of values to simulate MonteCarlo (from 1 to " + fileTreatment.MoC.size() + ")");
        } 
    }

	@Override
	public void keyPressed(KeyEvent e) {
		 if(e.getKeyCode() == KeyEvent.VK_ENTER) {
			 int value = Integer.parseInt(jtf.getText()) - 1;
			 if(value >= 0 && value < fileTreatment.MoC.size()) {
				print(fileTreatment);
	     		System.out.println("Monte Carlo result for Option (single)= " + fileTreatment.MoC.get(value).normal_compute());
	     		try {
					System.out.println("Monte Carlo result for Option (multi)= " + fileTreatment.MoC.get(value).thread_compute());
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			 }
		 }
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	public void print(FileTreatment fileTreatment) {
		for(int i = 0; i < fileTreatment.data().size(); i++) {
			System.out.println(fileTreatment.data().get(i).CallPutFlag + " " + fileTreatment.data().get(i).S
					+ " " + fileTreatment.data().get(i).X + " " + fileTreatment.data().get(i).r
					+ " " + fileTreatment.data().get(i).T + " " + fileTreatment.data().get(i).b
					+ " " + fileTreatment.data().get(i).v + " " + fileTreatment.data().get(i).nSteps
					+ " " + fileTreatment.data().get(i).nSimulations);
		}
	}
	
}
